package com.shopping.config;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
@MapperScan("com.shopping.dao")
public class WebConfig implements WebMvcConfigurer {
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("forward:/pages/login.html");
        registry.addViewController("/admin/login").setViewName("forward:/pages/adminLogin.html");
    }

    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/pages/**").addResourceLocations("classpath:/templates/pages/");
        registry.addResourceHandler("/images/userImg/**").addResourceLocations("file:" + System.getProperty("user.dir") + System.getProperty("file.separator") + "images" + System.getProperty("file.separator")+ "userImg" + System.getProperty("file.separator"));
        registry.addResourceHandler("/images/busImg/**").addResourceLocations("file:" + System.getProperty("user.dir") + System.getProperty("file.separator") + "images" + System.getProperty("file.separator")+ "busImg" + System.getProperty("file.separator"));
        registry.addResourceHandler("/images/proImg/**").addResourceLocations("file:" + System.getProperty("user.dir") + System.getProperty("file.separator") + "images" + System.getProperty("file.separator")+ "proImg" + System.getProperty("file.separator"));
    }

    public void addInterceptors(InterceptorRegistry registry) {
        // 添加不拦截的请求路径
        registry.addInterceptor(loginInterceptor())
                .excludePathPatterns("/api/login", "/api/register", "/api/forget","/api/selectMainProduct","/api/selectMainCate")
                .excludePathPatterns("/api/admin/login")
                .excludePathPatterns("/api/business/login", "/api/business/register","/api/business/forget")
                .excludePathPatterns("/swagger-resources/**", "/webjars/**", "/v2/**", "/swagger-ui.html/**")
                .excludePathPatterns("/")
                .addPathPatterns("/**");
    }

    @Bean
    public LoginInterceptor loginInterceptor() {
        return new LoginInterceptor();
    }

}
